package com.hckj.jianzhijia.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hckj.jianzhijia.CompanyActivity;
import com.hckj.jianzhijia.FeedBackActivity;
import com.hckj.jianzhijia.IdentificationActivity;
import com.hckj.jianzhijia.JianzhijiaApplacation;
import com.hckj.jianzhijia.MyIdentificationActivity;
import com.hckj.jianzhijia.R;
import com.hckj.jianzhijia.SettingActivity;
import com.hckj.jianzhijia.WatchMoreActivity;
import com.hckj.jianzhijia.WebPageActivity;
import com.hckj.jianzhijia.interfaces.UserInfoChangeInterface;
import com.hckj.jianzhijia.model.CompanyUser;
import com.hckj.jianzhijia.views.RoundImageView;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class MainCompanyFragment extends Fragment implements UserInfoChangeInterface {

    @InjectView(R.id.rl_toolbar)
    RelativeLayout rlToolbar;
    @InjectView(R.id.imgv_logo)
    RoundImageView imgvLogo;
    @InjectView(R.id.tv_name)
    TextView tvName;
    @InjectView(R.id.tv_email)
    TextView tvEmail;
    @InjectView(R.id.rl_base_info)
    RelativeLayout rlBaseInfo;
    @InjectView(R.id.rl_callback)
    RelativeLayout rlCallback;
    @InjectView(R.id.rl_help)
    RelativeLayout rlHelp;
    @InjectView(R.id.rl_more)
    RelativeLayout rlMore;
    @InjectView(R.id.rl_setting)
    RelativeLayout rlSetting;
    @InjectView(R.id.tv_goto_verify_name)
    TextView tvGotoVerifyName;
    @InjectView(R.id.rl_logo)
    RelativeLayout rlLogo;
    @InjectView(R.id.tv_certification)
    TextView tvCertification;
    @InjectView(R.id.rl_imgv_identity)
    RelativeLayout rlImgvIdentity;

    private View parentView;

    public MainCompanyFragment() {

    }

    public static MainCompanyFragment newInstance(View parentView) {
        MainCompanyFragment fragment = new MainCompanyFragment();
        Bundle args = new Bundle();
        fragment.setParentView(parentView);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
        ((JianzhijiaApplacation) getActivity().getApplication()).addUserInfoChange(this);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_company, container, false);
        ButterKnife.inject(this, view);
        initData();
        initEvent();
        return view;
    }

    private void initEvent() {

        rlImgvIdentity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyIdentificationActivity.class));
            }
        });

        rlBaseInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CompanyActivity.class));
            }
        });

        rlCallback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FeedBackActivity.class));
            }
        });

        rlHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WebPageActivity.class);
                intent.putExtra("WebPageType", WebPageActivity.WEBPAGE_HELP);
                startActivity(intent);
            }
        });

        rlMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), WatchMoreActivity.class));
            }
        });

        rlSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
        CompanyUser companyUser = ((JianzhijiaApplacation)getActivity().getApplication()).getCompanyUser();
        if (!companyUser.isIdentified()) {
            rlLogo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), IdentificationActivity.class));
                }
            });
        }

    }

    private void initData() {
        CompanyUser companyUser = ((JianzhijiaApplacation)getActivity().getApplication()).getCompanyUser();
        if (companyUser.getLogo() != null)
            Picasso.with(getActivity()).load(companyUser.getLogo().getUrl()).into(imgvLogo);
        if (companyUser.isIdentified()) {
            tvGotoVerifyName.setVisibility(View.GONE);
            tvCertification.setText("认证机构");
        } else {
            tvCertification.setText("未认证");
        }
        tvName.setText(companyUser.getName());
        tvEmail.setText(companyUser.getEmail());

    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    public void setParentView(View parentView) {
        this.parentView = parentView;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }


    @Override
    public void updateCompanyInfo() {
        tvName.setText(((JianzhijiaApplacation)getActivity().getApplication()).getCompanyUser().getName());
        tvEmail.setText(((JianzhijiaApplacation)getActivity().getApplication()).getCompanyUser().getEmail());
    }

    @Override
    public void updateCompanyLogo() {
        CompanyUser companyUser = ((JianzhijiaApplacation)getActivity().getApplication()).getCompanyUser();
        if (companyUser.getLogo() != null)
            Picasso.with(getActivity()).load(companyUser.getLogo().getUrl()).into(imgvLogo);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((JianzhijiaApplacation) getActivity().getApplication()).removeUserInfoChangeInterface(this);

    }
}
